package com.jinsi.service.yrf.iml;
import com.jinsi.constant.YrfConstant;
import com.jinsi.mapper.yrf.UserMapper;
import com.jinsi.pojo.SyUser;
import com.jinsi.pojo.Role;
import com.jinsi.pojo.UserRole;
import com.jinsi.service.yrf.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public SyUser getUserByNameAndPwd(String userName) {
        SyUser userByNameAndPwd = userMapper.getUserByNameAndPwd(userName, null);
        System.out.println(userByNameAndPwd);
        return userByNameAndPwd;
    }

    @Override
    public SyUser getUserByPhone(String userPhone) {
        return userMapper.getUserByPhone(userPhone);
    }

    @Override
    public List<UserRole> listByUserId(int userId) {
        return userMapper.listByUserId(userId);
    }

    @Override
    public Integer addUser(SyUser syUser) throws Exception{
        syUser.setDepartmentId(YrfConstant.DEPTMENTID);
        syUser.setUserCreateDate(new Date());
        Integer num = userMapper.addUser(syUser);
        if (num == 0){
            throw new Exception();
        }
        return syUser.getUserId();
    }


    @Override
    public Role selectById(int roleId) {
        return userMapper.selectById(roleId);
    }

    @Override
    public Integer addUserRole(Integer userId) throws Exception{
        Integer num = userMapper.addUserRole(userId,YrfConstant.ROLEID);
        if (num == 0){
            throw new Exception();
        }

        return userId;
    }
}
